var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), 
i = require("../../../utils/arrayUtil.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    serviceList:[],
    goodsList:[],
    pageSize:10,
    pageIndex:1,
    longitude:0,
    latitude:0,
    type_id: 0,
    estateId:0,
    keyword:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type_id){
      this.data.type_id = parseInt(options.type_id)
      this.setData({
        type_id: parseInt(options.type_id)
      })
    }    
  },
  onShow:function(){
    this.data.estateId = app.getStorageSync('estate_id');
    this.data.pageIndex = 1;
    this.setData({
      serviceList: [],
      goodsList: [],
    })
    this.onHttpInit()
  },
  onChange(e){
    console.log('onChange', e)
    this.data.keyword = e.detail;
  },
  onSearch(e){
    console.log('onSearch', e)
    this.onHttpInit();
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost("/product/type/estate/point/list", {
      estateId: that.data.estateId,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      keyword: that.data.keyword,
    }, function (res) {
      console.log("获取商家服务类型返回数据:", res)
      if(res.data.code == 0){
        if(res.data.data.types.length > 0){
          that.data.type_id = res.data.data.types[0].id;
        }
        that.setData({
          serviceList: res.data.data.types,
          goodsList: res.data.data.products,
        });
      }
    }, function (a) {
      console.log("获取商家服务类型返回数据 err:", a);
    });
  },
  handleType(e){
    console.log('handleType',e)
    var type_id = e.currentTarget.dataset.id;
    console.log('type_id', type_id);
    this.data.pageIndex = 1
    this.setData({
      goodsList:[],
      isLoading:true,
    })
    this.onClickServiceType(type_id);
  },
  onClickServiceType: function (type_id) {
    // console.log('onClickServiceType',e)
    // var type_id = e.target.dataset.id;
    var that = this
    fetchApi.httpPost("/product/type/point/product/list", {
      type_id:type_id,
      estateId: that.data.estateId,
      pageSize:that.data.pageSize,
      pageIndex: that.data.pageIndex,
      keyword:that.data.keyword,
    }, function (res) {
      console.log("获取商家服务类型返回数据:", res)
      if(res.data.code == 0){
        var products = res.data.data.products;
        if(products.length > 0){
          for (var index in products) {
            that.data.goodsList.splice(that.data.goodsList.length, 0, products[index]);
          }
          that.setData({
            hasData: true,
            goodsList: that.data.goodsList
          })
        }
      }
      
      that.setData({
        isLoading: false,
      })

    }, function (a) {
      console.log("获取商家服务类型返回数据 err:", a);
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.data.pageIndex = 1
    this.setData({
      goodsList:[],
      isLoading:true,
    })
    this.onClickServiceType(this.data.type_id);
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.onClickServiceType(this.data.type_id);
  },
})